var config = require('home:widget/ui/config/config.js');
function BankCardLayer() {
    this.init();
}
BankCardLayer.prototype.init = function() {
	var me = this;
	var template = __inline("./BankCardLayer.tmpl");
    var bank_list = config.zrbConf.bank_list;
    var content = template({bank_list:bank_list});
    $(document.body).append(content);
    $("select").bselect({ searchInput : false });
};
BankCardLayer.prototype.setProperty = function(bankcard_id,bank_type,bankcard_number) {
    var me = this;
    me.bankcard_id = bankcard_id;
    me.bank_type = bank_type;
    me.bankcard_number = bankcard_number;
}
BankCardLayer.prototype.show = function() {
    var me = this;
	$('#BankCardLayer').modalBox({
        keyClose:true,
        overlay:true,
        bodyClose:false
    });
    var selector = "#bank_type option[value='" + me.bank_type + "']";
    $(selector).attr('selected', true);
    $('.bselect-label').text(config.zrbConf.bank_list[me.bank_type]);
    $('#bankcard_number').val(me.bankcard_number);
    $('.close_btn').click(function () {
        $('#BankCardLayer').modalBox('close');
    });
    $('#BankCardLayer .form_el input').change(function() {
        me._checkForm(event.target);
    });
}
BankCardLayer.prototype.hide = function() {
    $('#BankCardLayer').modalBox('close');
}
BankCardLayer.prototype.setError = function(error_message) {
    $('#BankCardLayer .info').text(error_message);
    $('#BankCardLayer .info').css('color','#F90');
}
BankCardLayer.prototype.resetError = function() {
    $('#BankCardLayer .info').text('');
    $('#BankCardLayer .info').css('color','#000');
}
BankCardLayer.prototype.checkForm = function(){
    var me = this;
    var bank_type = $('#bank_type').val();
    var bankcard_number = $('#bankcard_number').val();
    var input_list = $('#BankCardLayer .form_el input');
    for(var i=0; i < input_list.length; i++) {
        var one_el = input_list[i];
        var status = me._checkForm(one_el);
        if(!status) {
            return false;
        }
    }
    me.resetError();
    return true;
}

BankCardLayer.prototype._checkForm=function(html) {
    var me = this;
    var id = $(html).attr('id');
    var jId = '#' + id;
    me.clearMessage($(jId));
    if(id == 'bank_type') {
        if(!$(jId).val()) {
            me.setMessage($(jId), '请输入银行名称。');
            return false;
        }
    } else if(id == 'bankcard_number') {
        var bankcard_number = $(jId).val();
        if(!bankcard_number) {
            me.setMessage($(jId),'请输入银行卡号。');
            return false;
        }
        if(/(^\d{15,20}$)/.test(bankcard_number) == false) {
            me.setMessage($(jId),'请输入正确的银行卡号');
            return false;
        }
    }
    return {'bank_type':bank_type,'bankcard_number':bankcard_number};
};
BankCardLayer.prototype.clearMessage =function($container) {
    $container.parent().parent().find('.input_info').text('');
};
BankCardLayer.prototype.setMessage = function($container,message,type) {
    $('.form_el .input_info').text('');
    type = type || 'error_msg';
    $infoContainer = $container.parent().parent().find('.input_info');
    $infoContainer.text(message);
    $infoContainer.removeClass('error_msg');
    $infoContainer.removeClass('info_msg');
    $infoContainer.addClass(type);
}
module.exports = BankCardLayer;